package site.param.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import site.param.service.monitor.IMonitorService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Administrator on 2017/1/3.
 */
@Controller
public class IndexController {
    @Autowired
    private IMonitorService monitorService;
    @Value("${login.user.name}")
    private String userName;
    @Value("${login.user.passwd}")
    private String passwd;
    @Value("${login.time.out}")
    private int timeOut;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public String forward(ModelMap map){
        //map.addAttribute("monitorBean",monitorService.systemInfo());
        return "login";
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(ModelMap map){
        //map.addAttribute("monitorBean",monitorService.systemInfo());
        return "index";
    }



    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public void login(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "user", required = true) String user ,
                      @RequestParam(value = "pwd", required = true) String pwd){
        boolean flag=false;

        if (userName.equals(user)&&passwd.equals(pwd)){
            request.getSession().setMaxInactiveInterval(timeOut);
            request.getSession().setAttribute("userName",userName);
            request.getSession().setAttribute("passwd",passwd);
            flag=true;
        }
        try {
            response.sendRedirect("index");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return flag==true?"index":"login";
    }

}
